﻿using H2.NET.Common;
using H2.NET.Web.BI.Model;
using H2.NET.Web.Workflow.DwzCode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace H2.NET.Web.BI.Report
{
    public partial class Index : BasePage
    {
        public List<bi_DB> DB = new List<bi_DB>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (AksRequest.GetQueryString("action") == "check")
            {
                String tbls = AksRequest.GetFormString("tbls");
                String tname = AksRequest.GetFormString("tname");
                List<String> tblList = tbls.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries).ToList();
                int size = 0;
                using (var tmpContext = new UltimusMDC3Entities())
                {
                    var currentTable = tmpContext.bi_Table.Where(t =>t.bi_DB.name + ".dbo." + t.table_name == tname).FirstOrDefault();
                    if (currentTable != null)
                    {
                        size = tmpContext.bi_Relation.Where(r => (r.bi_Table.id == currentTable.id && tblList.Contains(r.bi_Table1.bi_DB.name + ".dbo." + r.bi_Table1.table_name) || (r.bi_Table1.id == currentTable.id && tblList.Contains(r.bi_Table.bi_DB.name + ".dbo." + r.bi_Table.table_name)))).Count();
                    }
                }
                if (size > 0)
                {
                    SuccessMsg("OwnerIndex");
                }
                else
                {
                    ErrorMsg("OwnerIndex");
                }
                return;
            }
            if (AksRequest.GetQueryString("action") == "initTbls")
            {
                String tbls = AksRequest.GetFormString("tbls");
                List<String> tblList = tbls.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                String result = "";
                using (var tmpContext = new UltimusMDC3Entities())
                {
                    int size = 0;
                    foreach (var tname in tblList)
                    {
                        var currentTable = tmpContext.bi_Table.Where(t => t.bi_DB.name + ".dbo." + t.table_name == tname).FirstOrDefault();
                        size = tmpContext.bi_Relation.Where(r => (r.bi_Table.id == currentTable.id && tblList.Contains(r.bi_Table1.bi_DB.name + ".dbo." + r.bi_Table1.table_name) || (r.bi_Table1.id == currentTable.id && tblList.Contains(r.bi_Table.bi_DB.name + ".dbo." + r.bi_Table.table_name)))).Count();
                        if (size > 0)
                        {
                            result += tname + ",";
                        }
                    }
                }
                if (String.IsNullOrEmpty(result))
                {
                    result = tblList.First()+",";
                }
                SuccessMsg(result);
                return;
            }
            UltimusMDC3Entities context = new UltimusMDC3Entities();
            DB = context.bi_DB.ToList();
        }
    }
}